package com.eascs.web.o2o.web.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.common.util.IoUtil;
import com.eascs.notify.constant.ModelResult;
import com.eascs.notify.email.EmailSender;
import com.eascs.notify.email.entity.EmailContentEntity;
import com.eascs.notify.email.entity.EmailSourceSetting;
import com.eascs.notify.mail.MailNotifier;
import com.eascs.notify.mail.MailNotifyRequest;
import com.eascs.notify.mail.MailNotifyResponse;
import com.eascs.notify.mail.MailSendType;
import com.eascs.web.o2o.web.util.SystemType;

@Service
public class BuildEmailService {
	
	protected final Logger logger =LoggerFactory.getLogger(getClass());
	
	
	@Autowired
	EmailSourceSetting emailSourceSetting;
	@Autowired
	private EmailSender emailSender;
	
	@Value("${email.toemail.dev}")
	String devmail;
	
	@Value("${email.toemail.refund}")
	String refundmail;
	
	@Reference
	MailNotifier mailNotifier;
	
	/*
	 * 发送邮件--日邮件
	 */
	public void job_sendMail() {
		logger.info("=============开始发送邮件--日邮件===============");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ArrayList<String> listDirectories=new ArrayList<String>();
		String date = sdf.format(new Date());
		if(listDirectories==null||listDirectories.size()==0){
			IoUtil.createDirectorieIfMissing(new File(emailSourceSetting.getFilepath()+"/"+date).getParentFile());
			listDirectories.add(emailSourceSetting.getFilepath()+"/"+date); 
		}
		mailSendOneByOneNew(listDirectories, "O2O金融报表-",date,true);
		logger.info("=============结束发送邮件--日邮件===============");
	}
	
	
	
	/*
	 * 发送邮件,一个一个人的发送<br>
	 * 避免因为某个人的邮箱失效导致所有人都无法成功接受邮件
	 */
	public void mailSendOneByOne(ArrayList<String> listDirectories,String title,String date,boolean flag) {
		EmailContentEntity entity = new EmailContentEntity();
		entity.setHost(emailSourceSetting.getHost());
		entity.setUsername(emailSourceSetting.getFrom());
		entity.setPassword(emailSourceSetting.getPassword());
		entity.setFrom(emailSourceSetting.getFrom());
		entity.setSubject(title + date);
		entity.setContent(title + date);
		
		String[] receivers = emailSourceSetting.getTo().split(",");
		if(flag){
			//list去重
			HashSet<String> h = new  HashSet<String>(listDirectories); 
			listDirectories.clear(); 
			listDirectories.addAll(h); 
			entity.setAttachmentDirectories(listDirectories);
		}else{
			receivers = devmail.split(",");
		}
		
		for(String receiver : receivers){
			entity.setTo(receiver);
			emailSender.send(entity);
		}
		
	}


	
	/*
	 * 
	 * 发送邮件,一个一个人的发送<br>
	 * 避免因为某个人的邮箱失效导致所有人都无法成功接受邮件
	 */
	public void mailSendOneByOneNew(ArrayList<String> listDirectories,String title,String date,boolean flag) {
		MailNotifyRequest mailNotifyRequest=new MailNotifyRequest();
		mailNotifyRequest.setTitle(title + date);
		mailNotifyRequest.setContent(title + date);
		mailNotifyRequest.setSystemCode(SystemType.REPORT_MAIL);
		mailNotifyRequest.setMailSendType(MailSendType.INSTANT);
		String[] receivers = emailSourceSetting.getTo().split(",");
		if(flag){
			//list去重
			HashSet<String> h = new  HashSet<String>(listDirectories); 
			listDirectories.clear(); 
			listDirectories.addAll(h); 
			if(listDirectories!=null&&listDirectories.size()>0){
				mailNotifyRequest.setAttachmentPath(listDirectories.toString().substring(1, listDirectories.toString().length()-1));
			}
		}else{
			receivers = devmail.split(",");
		}
		ModelResult<MailNotifyResponse> model=null;
		for(String receiver : receivers){
			mailNotifyRequest.setRecipientTo(receiver);
			try {
				 model=mailNotifier.notify(mailNotifyRequest);
			} catch (Exception e) {
				logger.error("邮件发送异常，异常code为：[{}],异常内容为：[{}]", model.getErrCode(),model.getErrMsg());
			}
		}
		
	}
	
}
